//Copyright (c) 2010, Goodwine & Tibia ML
//Licensed under new BSD License.

// Choose where will the BB code be added.
var thread = document.getElementsByTagName('tbody');
for(var i=1; i < thread.length; i++)
{
	if(thread[i].innerHTML.match(/<td bgcolor="#505050".*?<b>(Message Preview|Thread Review \(newest first\)|(Edit )?Post Preview|Author)<\/b>/i))
	{
		post = thread[i].getElementsByTagName('tr');
		for(var j=1; j < post.length; j++)
		{
			enhacedBBCode(post[j].lastChild);
		}
	}
}

// Parse the text.
function enhacedBBCode(element)
{
	var pattern = [];
	var replace = [];
	
	// Patterns
	
	pattern[0] = /\[mlpage=(\d+?)\](.+?)\[\/mlpage\]/ig; //Tibia ML Pages //http://en.tibiaml.com/page/2764/
	
	// Text Styling
	pattern[1] = /\[color=#?([0-9A-F]{6})\]([\s\S]+?)\[\/color\]/ig;	//color
	pattern[2] = /\[s\]([\s\S]+?)\[\/s\]/ig;	//strike text

	// Link to Threads
	pattern[3] = /\[mlthread=(\d+?)\](.+?)\[\/mlthread\]/ig;	// Tibia ML Thread // http://en.tibiaml.com/forum/showthread.php?t=19319
	pattern[4] = /\[mxthread=(\d+?)\](.+?)\[\/mxthread\]/ig;	// TibiaMX Thread // http://www.tibiamx.com.mx/forums/viewtopic.php?t=654
	pattern[5] = /\[brthread=(\d+?)\](.+?)\[\/brthread\]/ig;	// TibiaBR Thread // http://forums.tibiabr.com/showthread.php?t=225175
	pattern[6] = /\[hsthread=(\d+?)\](.+?)\[\/hsthread\]/ig;	// TibiaHispano Thread // http://foros.tibiahispano.com/showthread.php?t=39963
	pattern[7] = /\[rpthread=(\d+?)\](.+?)\[\/rpthread\]/ig;	// TibiaRP Thread // http://www.tibiarp.com/forum/thread-1102.html
	pattern[8] = /\[tpthread=(\d+?)\](.+?)\[\/tpthread\]/ig;	// TibioPedia // http://tibiopedia.pl/forum/t/2945
	pattern[9] = /\[tcthread=(\d+?)\](.+?)\[\/tcthread\]/ig;	// TibiaCity // http://www.tibiacity.org/forum/showthread.php?14675

	// Link to Posts
	pattern[10] = /\[mlpost=(\d+?)\](.+?)\[\/mlpost\]/ig;	// Tibia ML Post // http://en.tibiaml.com/forum/showthread.php?p=19427
	pattern[11] = /\[mxpost=(\d+?)\](.+?)\[\/mxpost\]/ig;	// TibiaMX Post // http://www.tibiamx.com.mx/forums/viewtopic.php?p=37529
	pattern[12] = /\[brpost=(\d+?)\](.+?)\[\/brpost\]/ig;	// TibiaBr Post // http://forums.tibiabr.com/showthread.php?p=5475631
	pattern[13] = /\[hspost=(\d+?)\](.+?)\[\/hspost\]/ig;	// TibiaHispano Post // http://foros.tibiahispano.com/showthread.php?p=835403
	pattern[14] = /\[rppost=(\d+?)\](.+?)\[\/rppost\]/ig;	// TibiaRP Post // http://www.tibiarp.com/forum/post-16534.html
	pattern[15] = /\[tppost=(\d+?)\](.+?)\[\/tppost\]/ig;	// TibioPedia Post // http://tibiopedia.pl/forum/p/40984
	pattern[16] = /\[tcpost=(\d+?)\](.+?)\[\/tcpost\]/ig;	// TibiaCity Post // http://www.tibiacity.org/forum/showthread.php?p=217802

	// Lottos
	pattern[17] = /\[lottery=(\d+?)\](.+?)\[\/lottery\]/ig;	// http://tibialottery.com/view/28367
	pattern[18] = /\[mllottery=(\d+?)\](.+?)\[\/mllottery\]/ig;	// http://en.tibiaml.com/lottery/14482/

	// News & Pages
	pattern[19] = /\[tcnews=(\d+?)\](.+?)\[\/tcnews\]/ig;	// TibiaCity NewsBot // http://dev.tibiacity.org/newsbot.php?post=3051494
	pattern[20] = /\[tcpage=(\d+?)\](.+?)\[\/tcpage\]/ig;	// TibiaCity Pages // http://dev.tibiacity.org/p/26/
	pattern[21] = /\[tpnews=(\d+?)\](.+?)\[\/tpnews\]/ig;	// TibioPedia News // http://tibiopedia.pl/news/archives/271 /*reacts differently according to language*/
	pattern[22] = /\[hsnews=(\d+?)\](.+?)\[\/hsnews\]/ig;	// TibiaHispano News // http://www.tibiahispano.com/noticias/8739
	pattern[23] = /\[frpage=(\d+?)\](.+?)\[\/frpage\]/ig;	// TibiaFriends Pages // http://tibiafriends.com/pg/pages/view/73035/
	pattern[24] = /\[rppage=(\d+?)\](.+?)\[\/rppage\]/ig;	// TibiaRP Pages // http://www.tibiarp.com/home/news/id/467/
	pattern[25] = /\[mxnews=(\d+?)\](.+?)\[\/mxnews\]/ig;	// TibiaMX News // http://www.tibiamx.com.mx/portada/articulos/397

	// Images
	pattern[26] = /\[img\](https?:\/\/.+?)\[\/img\]/ig;	// Show images in forums
	
	// Replacements
	
	replace[0] = "<a title='Tibia ML' href='http://en.tibiaml.com/page/$1'>$2</a>"; 
	
	// Text Styles
	replace[1] = "<span style='color: #$1'>$2</span>";
	replace[2] = "<s>$1</s>";
	
	// Link to threads
	replace[3] = "<a title='Tibia ML Forum Thread' href='http://en.tibiaml.com/forum/showthread.php?t=$1'>$2</a>";
	replace[4] = "<a title='TibiaMx Forum Thread' href='http://www.tibiamx.com.mx/forums/viewtopic.php?t=$1'>$2</a>";
	replace[5] = "<a title='TibiaBR Forum Thread' href='http://forums.tibiabr.com/showthread.php?t=$1'>$2</a>";
	replace[6] = "<a title='TibiaHispano Forum Thread' href='http://foros.tibiahispano.com/showthread.php?t=$1'>$2</a>";
	replace[7] = "<a title='TibiaRP Forum Thread' href='http://www.tibiarp.com/forum/thread-$1.html'>$2</a>";
	replace[8] = "<a title='TibioPedia Forum Thread' href='http://tibiopedia.pl/forum/t/$1'>$2</a>";
	replace[9] = "<a title='TibiaCity Forum Thread' href='http://www.tibiacity.org/forum/showthread.php?$1'>$2</a>";
	
	// Link to Posts
	replace[10] = "<a title='Tibia ML Forum Post' href='http://en.tibiaml.com/forum/showthread.php?p=$1'>$2</a>";
	replace[11] = "<a title='TibiaMX Forum Post' href='http://www.tibiamx.com.mx/forums/viewtopic.php?p=$1'>$2</a>";
	replace[12] = "<a title='TibiaBR Forum Post' href='http://forums.tibiabr.com/showthread.php?p=$1'>$2</a>";
	replace[13] = "<a title='TibiaHispano Forum Post' href='http://foros.tibiahispano.com/showthread.php?p=$1'>$2</a>";
	replace[14] = "<a title='TibiaRP Forum Post' href='http://www.tibiarp.com/forum/post-$1.html'>$2</a>";
	replace[15] = "<a title='TibioPedia Forum Post' href='http://tibiopedia.pl/forum/p/$1'>$2</a>";
	replace[16] = "<a title='TibiaCity Forum Post' href='http://www.tibiacity.org/forum/showthread.php?p=$1'>$2</a>";
	
	// Lottos
	replace[17] = "<a title='TibiaLottery' href='http://tibialottery.com/view/$1'>$2</a>";
	replace[18] = "<a title='Tibia ML Lottery' href='http://en.tibiaml.com/lottery/$1'>$2</a>";
	
	// News & Pages
	replace[19] = "<a title='TibiaCity NewsBot' href='http://dev.tibiacity.org/newsbot.php?post=$1'>$2</a>";
	replace[20] = "<a title='TibiaCity' href='http://dev.tibiacity.org/p/$1'>$2</a>";
	replace[21] = "<a title='TibioPedia' href='http://tibiopedia.pl/news/archives/$1'>$2</a>";
	replace[22] = "<a title='TibiaHispano' href='http://www.tibiahispano.com/noticias/$1'>$2</a>";
	replace[23] = "<a title='TibiaFriends' href='http://tibiafriends.com/pg/pages/view/$1'>$2</a>";
	replace[24] = "<a title='TibiaRP' href='http://www.tibiarp.com/home/news/id/$1'>$2</a>";
	replace[25] = "<a title='TibiaMX' href='http://www.tibiamx.com.mx/portada/articulos/$1'>$2</a>";
	
	// Images
	replace[26] = "<img style='max-width:600px; max-height:1000px;' src='$1'/>";
	
	for(var i = 0; i < pattern.length; i++)
		element.innerHTML = element.innerHTML.replace(pattern[i], replace[i]);
}